package org.rockyang.tio.core.client;

import org.rockyang.tio.core.server.HelloPacket;
import org.tio.client.ClientChannelContext;
import org.tio.client.ClientGroupContext;
import org.tio.client.ReconnConf;
import org.tio.client.TioClient;
import org.tio.client.intf.ClientAioHandler;
import org.tio.client.intf.ClientAioListener;
import org.tio.core.Node;
import org.tio.core.Tio;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;

/**
 * @author tanyaowu
 */
public class HelloClientStarter {
	//服务器节点
	public static Node serverNode = new Node(Const.SERVER, Const.PORT);

	//handler, 包括编码、解码、消息处理
	public static ClientAioHandler tioClientHandler = new HelloClientAioHandler();

	//事件监听器，可以为null，但建议自己实现该接口，可以参考showcase了解些接口
	public static ClientAioListener aioListener = null;

	//断链后自动连接的，不想自动连接请设为null
	private static ReconnConf reconnConf = new ReconnConf(5000L);

	//一组连接共用的上下文对象
	public static ClientGroupContext clientGroupContext = new ClientGroupContext(tioClientHandler, aioListener, reconnConf);

	public static TioClient tioClient = null;
	public static ClientChannelContext clientChannelContext = null;

	/**
	 * 启动程序入口
	 */
	public static void main3(String[] args) throws Exception {
		clientGroupContext.setHeartbeatTimeout(Const.TIMEOUT);
		tioClient = new TioClient(clientGroupContext);
		clientChannelContext = tioClient.connect(serverNode);
		//连上后，发条消息玩玩
		send();
	}
	public static void main2(String[] args) {
		try {
//1.建立客户端socket连接，指定服务器位置及端口
			Socket socket = new Socket("127.0.0.1", 6789);
//2.得到socket读写流
			OutputStream os = socket.getOutputStream();
			PrintWriter pw = new PrintWriter(os);
//输入流
			InputStream is = socket.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
//3.利用流按照一定的操作，对socket进行读写操作
			String sendInfo = "向服务器发送的数据信息！";
			pw.write(sendInfo);
			pw.flush();
			socket.shutdownOutput();
//接收服务器的相应
			String replyInfo = null;
			while (!((replyInfo = br.readLine()) == null)) {
				System.out.println("接收服务器的数据信息：" + replyInfo);
			}
//4.关闭资源
			br.close();
			is.close();
			pw.close();
			os.close();
			socket.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	public static void main(String[] args) {
		try (SocketChannel socketChannel = SocketChannel.open()) {
			//连接服务端socket
			SocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 6789);
			socketChannel.connect(socketAddress);

			int sendCount = 0;

			ByteBuffer buffer = ByteBuffer.allocate(1024);

			buffer.clear();
			//向服务端发送消息
			buffer.put(("current time : " + System.currentTimeMillis()).getBytes());
			//读取模式
			buffer.flip();
			socketChannel.write(buffer);
			buffer.clear();

		} catch (IOException e) {
			e.printStackTrace();
		}

	}


	private static void send() throws Exception {
		HelloPacket packet = new HelloPacket();
		packet.setBody("[35380100360174,898600MFSSYYGXXXXXXP12345,202007111620110,DEVICE_LOGIN,3,20200711162011,12,5@1@1@0@1@21]".getBytes(HelloPacket.CHARSET));
		Tio.send(clientChannelContext, packet);
	}
}
